/**********************************************************************************************************************
 * \file STM_Interrupt.c
 * \copyright Copyright (C) Infineon Technologies AG 2019
 *
 * Use of this file is subject to the terms of use agreed between (i) you or the company in which ordinary course of
 * business you are acting and (ii) Infineon Technologies AG or its licensees. If and as long as no such terms of use
 * are agreed, use of this file is subject to following:
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and
 * accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute,
 * and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the
 * Software is furnished to do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including the above license grant, this restriction
 * and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all
 * derivative works of the Software, unless such copies or derivative works are solely in the form of
 * machine-executable object code generated by a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *********************************************************************************************************************/

/*********************************************************************************************************************/
/*-----------------------------------------------------Includes------------------------------------------------------*/
/*********************************************************************************************************************/

#include "Bsp.h"
#include "IfxPort.h"
#include "IfxStm.h"
#include "drv_led.h"
#include "drv_stm.h"
#include "os_user.h"
#include "portmacro.h"
#include "srv_uart.h"
#include "stddef.h"
#include "FreeRTOSConfig.h"
#include "lwipopts.h"
#include "os_user.h"
/*********************************************************************************************************************/
/*------------------------------------------------------Macros-------------------------------------------------------*/
/*********************************************************************************************************************/
#define ISR_PRIORITY_STM0        40                              /* Priority for interrupt ISR                       */
#define ISR_PRIORITY_STM1        41
#define TIMER_INT_TIME          1                             /* Time between interrupts in ms                    */

#define STM0                     &MODULE_STM0                    /* STM0 is used in this example                     */
#define STM1                     &MODULE_STM1
/*********************************************************************************************************************/
/*-------------------------------------------------Global variables--------------------------------------------------*/
/*********************************************************************************************************************/
IfxStm_CompareConfig g_STMConf0;                                 /* STM configuration structure                      */
IfxStm_CompareConfig g_STMConf1;
Ifx_TickTime g_ticksFor1ms;                                   /* Variable to store the number of ticks to wait    */

uint32_t systick_200ms_cpu0 = 0u;


uint32_t systick_1ms_cpu1 = 0u;
uint32_t systick_10ms_cpu1 = 0u;
uint32_t systick_100ms_cpu1 = 0u;
uint32_t systick_200ms_cpu1 = 0u;
uint32_t systick_1000ms_cpu1 = 0u;

/*********************************************************************************************************************/
/*------------------------------------------------Function Prototypes------------------------------------------------*/
/*********************************************************************************************************************/

extern   void STM0_ISR();
extern   void STM1_ISR();
void initSTM0(void);

/*********************************************************************************************************************/
/*---------------------------------------------Function Implementations----------------------------------------------*/
/*********************************************************************************************************************/
/* Macro to define Interrupt Service Routine.
 * This macro makes following definitions:
 * 1) Define linker section as .intvec_tc<vector number>_<interrupt priority>.
 * 2) define compiler specific attribute for the interrupt functions.
 * 3) define the Interrupt service routine as ISR function.
 *
 * IFX_INTERRUPT(isr, vectabNum, priority)
 *  - isr: Name of the ISR function.
 *  - vectabNum: Vector table number.
 *  - priority: Interrupt priority. Refer Usage of Interrupt Macro for more details.
 */
//IFX_INTERRUPT(isrSTM0, 0, ISR_PRIORITY_STM0);
extern void prvSystemTickHandler();
IFX_INTERRUPT(isrSTM0, 0, ISR_PRIORITY_STM0);
IFX_INTERRUPT(isrSTM1, 1, ISR_PRIORITY_STM1);

void isrSTM0(void)
{
    systick_200ms_cpu0++;
#if 0
//    WATCH_STACK(4000)
    static uint32 cnt = 0;
    uint32*uCsaAddr = NULL;
    uint32*lCsaAddr = NULL;
    uint32 csa = 0;
        __dsync();
        csa = __mfcr( Tricore_PCXI );
        lCsaAddr = portCSA_TO_ADDRESS( csa );
        uCsaAddr = portCSA_TO_ADDRESS(lCsaAddr[0]);
        __isync();
        static int y = 0;
        cnt++;
        extern void Ifx_Lwip_printf(const char *format, ...);
        y++;


        if(y>1000)
        {
            y = 0;
            uart_print_async(1,csa);
            uart_print_async(2,lCsaAddr[1]);
            uart_print_async(3,uCsaAddr[3]);
//            drv_led2_toggle();
        }

        if(y== (1000))
        {
//            Ifx_Lwip_printf("lcsa = %X,",csa);
//            Ifx_Lwip_printf("return addr l = %X,",lCsaAddr[1]);
//            Ifx_Lwip_printf("return addr u = %X",uCsaAddr[3]);
//            Ifx_Lwip_printf("ucsa = %X,",lCsaAddr[0]);
        }

        else if(y == (2000))
        {
//           Ifx_Lwip_printf("SP = %X",uCsaAddr[2]);
        }
        else if(y ==  (3000))
        {
            //           Ifx_Lwip_printf("PSW = %X",uCsaAddr[1]);
        }
        else if(y ==  4001)
        {
           y = 0;
        }
#endif

        void increase_g_TickCount_1ms_isr(void);
        increase_g_TickCount_1ms_isr();
        /* Update the compare register value that will trigger the next interrupt and toggle the LED */
        IfxStm_increaseCompare(STM0, g_STMConf0.comparator, g_ticksFor1ms);
        STM0_ISR();
#if !NO_SYS
        if(b_os_initciallized)
        {
            prvSystemTickHandler();
        }
#endif
}
void isrSTM1(void)
{
    /* Update the compare register value that will trigger the next interrupt and toggle the LED */
    IfxStm_increaseCompare(STM1, g_STMConf1.comparator, g_ticksFor1ms);
    STM1_ISR();

}


/* Function to initialize the STM */
void initSTM0(void)
{
    g_ticksFor1ms = IfxStm_getTicksFromMilliseconds(BSP_DEFAULT_TIMER, TIMER_INT_TIME);
    IfxStm_initCompareConfig(&g_STMConf0);           /* Initialize the configuration structure with default values   */

    g_STMConf0.triggerPriority = ISR_PRIORITY_STM0;   /* Set the priority of the interrupt                            */
    g_STMConf0.typeOfService = IfxSrc_Tos_cpu0;      /* Set the service provider for the interrupts                  */
    g_STMConf0.ticks = g_ticksFor1ms;              /* Set the number of ticks after which the timer triggers an
                                                     * interrupt for the first time                                 */
    IfxStm_initCompare(STM0, &g_STMConf0);            /* Initialize the STM with the user configuration               */
}
void initSTM1(void)
{
    g_ticksFor1ms = IfxStm_getTicksFromMilliseconds(BSP_DEFAULT_TIMER, TIMER_INT_TIME);
    IfxStm_initCompareConfig(&g_STMConf1);           /* Initialize the configuration structure with default values   */

    g_STMConf1.triggerPriority = ISR_PRIORITY_STM1;   /* Set the priority of the interrupt                            */
    g_STMConf1.typeOfService = IfxSrc_Tos_cpu1;      /* Set the service provider for the interrupts                  */
    g_STMConf1.ticks = g_ticksFor1ms;              /* Set the number of ticks after which the timer triggers an
                                                     * interrupt for the first time                                 */
    IfxStm_initCompare(STM1, &g_STMConf1);            /* Initialize the STM with the user configuration               */
}
